﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using UpYourLife.Services.Models;
using System.Xml.Linq;
using UpYourLife.Services.Google;

namespace UpYourLife.Services
{
    public class ServiceRepository
    {
        private XmlDocument xmlDoc;
        private string xmlPath = "Data/UpYourLifeData.xml";
        public ServiceRepository()
        {
            xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlPath);

            //var xePlaces = XElement.Load(xmlPath).Descendants("Place").Where(x => x.Element("Type").Value == "CoffeeShop").ToList(); 

        }

        public Place GetPlace(Guid place_id)
        {
            XElement xePlace = XElement.Load(xmlPath).Descendants("Place").Where(x => x.Attribute("id").Value == place_id.ToString()).SingleOrDefault();

            Place place = new Place
            {
                PlaceID = place_id,
                PlaceName = xePlace.Element("PlaceName").Value,
                Type = xePlace.Element("Type").Value,
                AdressNumber = xePlace.Element("AddressNumber").Value,
                Stress = xePlace.Element("Stress").Value,
                Ward = xePlace.Element("Ward").Value,
                District = xePlace.Element("District").Value,
                City = xePlace.Element("City").Value,
                Description = xePlace.Element("Latitude").Value,
                Longitude = double.Parse(xePlace.Element("Longitude").Value),
                Latitude = double.Parse(xePlace.Element("Latitude").Value),
                MoreInformation = xePlace.Element("MoreInformation").Value,
                ImageUri = xePlace.Element("ImageUri").Value

            };

            return place;
        }

        public List<Place> GetPlaces(string type)
        {
            List<Place> xePlaces = (from x in XElement.Load(xmlPath).Descendants("Place")
                                    where x.Element("Type").Value == type
                                    select new Place
                                    {
                                        PlaceID = new Guid(x.Attribute("id").Value),
                                        PlaceName = x.Element("PlaceName").Value,
                                        Type = x.Element("Type").Value,
                                        AdressNumber = x.Element("AddressNumber").Value,
                                        Stress = x.Element("Stress").Value,
                                        Ward = x.Element("Ward").Value,
                                        District = x.Element("District").Value,
                                        City = x.Element("City").Value,
                                        Latitude = double.Parse(x.Element("Latitude").Value),
                                        Longitude = double.Parse(x.Element("Longitude").Value),
                                        Description = x.Element("Description").Value,
                                        MoreInformation = x.Element("MoreInformation").Value
                                    }).ToList();

            return xePlaces;
        }

        public List<Place> GetPlaces(string[] types)
        {
            List<Place> xeFinalPlaces = new List<Place>();
            foreach (string type in types)
            {
                if (string.IsNullOrEmpty(type))
                {
                    continue;
                }
                else
                {
                    List<Place> xePlaces = (from x in XElement.Load(xmlPath).Descendants("Place")
                                            where x.Element("Type").Value == type
                                            select new Place
                                            {
                                                PlaceID = new Guid(x.Attribute("id").Value),
                                                PlaceName = x.Element("PlaceName").Value,
                                                Type = x.Element("Type").Value,
                                                AdressNumber = x.Element("AddressNumber").Value,
                                                Stress = x.Element("Stress").Value,
                                                Ward = x.Element("Ward").Value,
                                                District = x.Element("District").Value,
                                                City = x.Element("City").Value,
                                                Latitude = double.Parse(x.Element("Latitude").Value),
                                                Longitude = double.Parse(x.Element("Longitude").Value),
                                                Description = x.Element("Description").Value,
                                                MoreInformation = x.Element("MoreInformation").Value,
                                                ImageUri = x.Element("ImageUri").Value
                                            }).ToList();
                    xeFinalPlaces.AddRange(xePlaces);
                }                               
            }

            return xeFinalPlaces;
        }

        public List<Place> GetPlaces()
        {
            List<Place> xePlaces = (from x in XElement.Load(xmlPath).Descendants("Place")
                                    select new Place
                                    {
                                        PlaceID = new Guid(x.Attribute("id").Value),
                                        PlaceName = x.Element("PlaceName").Value,
                                        Type = x.Element("Type").Value,
                                        AdressNumber = x.Element("AddressNumber").Value,
                                        Stress = x.Element("Stress").Value,
                                        Ward = x.Element("Ward").Value,
                                        District = x.Element("District").Value,
                                        City = x.Element("City").Value,
                                        Latitude = double.Parse(x.Element("Latitude").Value),
                                        Longitude = double.Parse(x.Element("Longitude").Value),
                                        Description = x.Element("Description").Value,
                                        MoreInformation = x.Element("MoreInformation").Value,
                                        ImageUri = x.Element("ImageUri").Value
                                    }).ToList();
            return xePlaces;
        }

        public Friend GetFriend(Guid friend_id)
        {
            XElement xeFriend = XElement.Load(xmlPath).Descendants("Friend").Where(x => x.Attribute("id").Value == friend_id.ToString()).SingleOrDefault();

            Friend friend = new Friend
            {
                FriendID = friend_id,
                FullName = xeFriend.Element("FullName").Value,
                Phone = xeFriend.Element("Phone").Value,
                Email = xeFriend.Element("Email").Value,
                Facebook = xeFriend.Element("Facebook").Value,
                Picture = xeFriend.Element("Picture").Value,                
                Longitude = double.Parse(xeFriend.Element("Longitude").Value),
                Latitude = double.Parse(xeFriend.Element("Latitude").Value),
                Description = xeFriend.Element("Latitude").Value,
                Password = xeFriend.Element("Password").Value, 
            };

            return friend;
        }

        public List<Friend> GetFriends()
        {
            List<Friend> xeFriends = (from x in XElement.Load(xmlPath).Descendants("Friend")
                                    select new Friend
                                    {
                                        FriendID = new Guid(x.Attribute("id").Value),
                                        FullName = x.Element("FullName").Value,
                                        Email = x.Element("Email").Value,
                                        Phone = x.Element("Phone").Value,
                                        Facebook = x.Element("Facebook").Value,
                                        Picture = x.Element("Picture").Value,                                        
                                        Latitude = double.Parse(x.Element("Latitude").Value),
                                        Longitude = double.Parse(x.Element("Longitude").Value),
                                        Description = x.Element("Description").Value,
                                        Password = x.Element("Password").Value,
                                        IsChecked = false,
                                        MetaInfo = string.Format("{0}@{1}", x.Attribute("id").Value, x.Element("FullName").Value)
                                    }).ToList();
            return xeFriends;
        }

        public GoogleLocalResponseData GetGoogleLocalResponseData(string keyword)
        {
            GoogleLocalResponseData results = LocalSearchApi.SearchLocal(keyword, 0, "www.microsoft.com");
            return results;
        }

        public void CreateMessage(UpMessage upMessage)
        {
            XDocument xDoc = XDocument.Load(xmlPath);
            //XElement xMessagesElement = XElement.Load(xmlPath).Element("Messages");
            XElement xMessagesElement = xDoc.Descendants("Messages").Single();

            XElement xMessageElement = new XElement("Message", 
                new XAttribute("id", upMessage.UpMessageID.ToString()),
                new XElement("From", upMessage.From.ToString()),
                new XElement("FromName", upMessage.FromName),
                new XElement("To", upMessage.To.ToString()),
                new XElement("ToName", upMessage.ToName),
                new XElement("Content", upMessage.Content),
                new XElement("Type", upMessage.Type),
                new XElement("MessageTime", upMessage.MessageTime),
                new XElement("ScheduleTime", upMessage.ScheduleTime),
                new XElement("LocationName", upMessage.LocationName),
                new XElement("Latitude", upMessage.Latitude),
                new XElement("Longitude", upMessage.Longitude));

            xMessagesElement.Add(xMessageElement);

            //xMessagesElement.Save(xmlPath, SaveOptions.None);
            xDoc.Save(xmlPath);
        }
    }
}
